#include "TcpServer.h"
#include <iostream>

using std::cout;
using std::endl;

// TcpServer::TcpServer(const string &ip, unsigned short port, int subReactorNum, int threadNum, int queueSize)
TcpServer::TcpServer(const string &ip, unsigned short port, int subReactorNum, ThreadPool *threadPool)
: _mainReactor(ip, port, subReactorNum)
, _threadPool(threadPool)
{
}

TcpServer::~TcpServer()
{

}

void TcpServer::start()
{
    _threadPool->start();
    _mainReactor.start();
}

void TcpServer::stop()
{
    _mainReactor.stop();
    _threadPool->stop();
}

void TcpServer::setAllCallback(TcpConnectionCallback &&cb1, 
                    TcpConnectionCallback &&cb2,
                    TcpConnectionCallback &&cb3)
{
    _mainReactor.setAllCallback(std::move(cb1), std::move(cb2), std::move(cb3));
}

// 定时器接口实现
int64_t TcpServer::runAfter(std::chrono::milliseconds delay, TimerCallback &&callback)
{
    return _mainReactor.runAfter(delay, std::move(callback));
}

int64_t TcpServer::runEvery(std::chrono::milliseconds interval, TimerCallback &&callback)
{
    return _mainReactor.runEvery(interval, std::move(callback));
}

void TcpServer::cancelTimer(int64_t timerId)
{
    _mainReactor.cancelTimer(timerId);
}

// 负载均衡接口实现
void TcpServer::setLoadBalanceStrategy(LoadBalanceStrategy strategy)
{
    _mainReactor.setLoadBalanceStrategy(strategy);
}

LoadBalanceStrategy TcpServer::getLoadBalanceStrategy() const
{
    return _mainReactor.getLoadBalanceStrategy();
}

std::pair<size_t, size_t> TcpServer::getConnectionRange()
{
    return _mainReactor.getConnectionRange();
}

// 连接数管理接口实现
void TcpServer::setMaxConnections(size_t maxConns)
{
    _mainReactor.setMaxConnections(maxConns);
}

size_t TcpServer::getMaxConnections() const
{
    return _mainReactor.getMaxConnections();
}

